package com.tencent.liteav.demo.superplayer.ui.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.tencent.liteav.demo.superplayer.R;


/**
 * Created by yuejiaoli on 2018/7/4.
 * 更多选项弹框
 */

public class UUSpeedRateMoreView extends RelativeLayout implements RadioGroup.OnCheckedChangeListener {
    private Context mContext;
    private Callback mCallback;
    private RadioGroup mRadioGroup;
    private RadioButton mRbSpeed05;
    private RadioButton mRbSpeed1;
    private RadioButton mRbSpeed12;
    private RadioButton mRbSpeed15;
    private RadioButton mRbSpeed2;

    private ImageView iv_speed2, iv_speed15, iv_speed12, iv_speed1, iv_speed05;

    private float speedLevel = 1.0f;
    private int speedColor = Color.parseColor("#33CCCC");

    public void setSpeedLevel(float speedLevel) {
        this.speedLevel = speedLevel;
        changeSpeed();
    }

    public void setSpeedColor(int speedColor){
        this.speedColor = speedColor;
        changeColor();
    }

    public UUSpeedRateMoreView(Context context) {
        super(context);
        init(context);
    }

    public UUSpeedRateMoreView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public UUSpeedRateMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.uu_player_more_popup_view, this);

        mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        mRbSpeed05 = (RadioButton) findViewById(R.id.rb_speed05);
        mRbSpeed1 = (RadioButton) findViewById(R.id.rb_speed1);
        mRbSpeed12 = (RadioButton) findViewById(R.id.rb_speed12);
        mRbSpeed15 = (RadioButton) findViewById(R.id.rb_speed15);
        mRbSpeed2 = (RadioButton) findViewById(R.id.rb_speed2);

        mRadioGroup.setOnCheckedChangeListener(this);

        iv_speed2 = findViewById(R.id.iv_speed2);
        iv_speed15 = findViewById(R.id.iv_speed15);
        iv_speed12 = findViewById(R.id.iv_speed12);
        iv_speed1 = findViewById(R.id.iv_speed1);
        iv_speed05 = findViewById(R.id.iv_speed05);

        changeSpeed();
        changeColor();

    }

    private void changeSpeed() {
        if (speedLevel == 0.5f) {
            if (mRbSpeed05 != null) {
                mRbSpeed05.setChecked(true);
            }
        } else if (speedLevel == 1.0f) {
            if (mRbSpeed1 != null) {
                mRbSpeed1.setChecked(true);
            }
        } else if (speedLevel == 1.2f) {
            if (mRbSpeed12 != null) {
                mRbSpeed12.setChecked(true);
            }
        } else if (speedLevel == 1.5f) {
            if (mRbSpeed15 != null) {
                mRbSpeed15.setChecked(true);
            }
        } else if (speedLevel == 2.0f) {
            if (mRbSpeed2 != null) {
                mRbSpeed2.setChecked(true);
            }
        }
    }

    private void changeColor(){
        ColorStateList colorStateList = new ColorStateList(new int[][]
                {{-android.R.attr.state_checked},
                        {android.R.attr.state_checked}},
                new int[]{Color.parseColor("#ffffff"), speedColor});
        mRbSpeed05.setTextColor(colorStateList);
        mRbSpeed1.setTextColor(colorStateList);
        mRbSpeed12.setTextColor(colorStateList);
        mRbSpeed15.setTextColor(colorStateList);
        mRbSpeed2.setTextColor(colorStateList);
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        iv_speed2.setVisibility(View.INVISIBLE);
        iv_speed15.setVisibility(View.INVISIBLE);
        iv_speed12.setVisibility(View.INVISIBLE);
        iv_speed1.setVisibility(View.INVISIBLE);
        iv_speed05.setVisibility(View.INVISIBLE);

        if (checkedId == R.id.rb_speed05) {
            mRbSpeed05.setChecked(true);
            iv_speed05.setVisibility(View.VISIBLE);
            if (mCallback != null) {
                mCallback.onSpeedChange(0.5f);
            }

        } else if (checkedId == R.id.rb_speed1) {
            mRbSpeed1.setChecked(true);
            iv_speed1.setVisibility(View.VISIBLE);
            if (mCallback != null) {
                mCallback.onSpeedChange(1.0f);
            }

        } else if (checkedId == R.id.rb_speed12) {
            mRbSpeed12.setChecked(true);
            iv_speed12.setVisibility(View.VISIBLE);
            if (mCallback != null) {
                mCallback.onSpeedChange(1.2f);
            }

        } else if (checkedId == R.id.rb_speed15) {
            mRbSpeed15.setChecked(true);
            iv_speed15.setVisibility(View.VISIBLE);
            if (mCallback != null) {
                mCallback.onSpeedChange(1.5f);
            }

        } else if (checkedId == R.id.rb_speed2) {
            mRbSpeed2.setChecked(true);
            iv_speed2.setVisibility(View.VISIBLE);
            if (mCallback != null) {
                mCallback.onSpeedChange(2.0f);
            }

        }
    }

    public interface Callback {
        void onSpeedChange(float speedLevel);
    }

}
